<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻头条查询</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 30px;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            border-radius: 15px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
            position: relative;
            overflow: hidden;
        }
        
        header::before {
            content: "";
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
            transform: rotate(30deg);
        }
        
        h1 {
            font-size: 2.8rem;
            margin-bottom: 15px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.2);
            position: relative;
        }
        
        .description {
            font-size: 1.2rem;
            opacity: 0.9;
            max-width: 800px;
            margin: 0 auto;
            position: relative;
        }
        
        .controls {
            display: flex;
            justify-content: center;
            margin-bottom: 30px;
            flex-wrap: wrap;
            gap: 15px;
            position: relative;
        }
        
        .category-btn {
            padding: 12px 25px;
            background-color: #fff;
            border: 2px solid #2575fc;
            border-radius: 30px;
            color: #2575fc;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 8px rgba(37, 117, 252, 0.2);
        }
        
        .category-btn:hover {
            background-color: #2575fc;
            color: white;
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(37, 117, 252, 0.3);
        }
        
        .category-btn.active {
            background-color: #2575fc;
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(37, 117, 252, 0.3);
        }
        
        .news-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 30px;
        }
        
        .news-card {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
            transition: transform 0.4s ease, box-shadow 0.4s ease;
            position: relative;
        }
        
        .news-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
        }
        
        .news-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
            background: linear-gradient(45deg, #eef2f7, #dbe6f6);
            display: flex;
            align-items: center;
            justify-content: center;
            color: #6c757d;
            font-weight: bold;
            position: relative;
            overflow: hidden;
        }
        
        .news-image::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(rgba(255,255,255,0.3), rgba(255,255,255,0.1));
        }
        
        .news-content {
            padding: 25px;
        }
        
        .news-title {
            font-size: 1.35rem;
            margin-bottom: 15px;
            color: #2c3e50;
            line-height: 1.4;
        }
        
        .news-meta {
            display: flex;
            justify-content: space-between;
            color: #6c757d;
            font-size: 0.95rem;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 1px solid #f0f4f8;
        }
        
        .news-description {
            color: #555;
            margin-bottom: 20px;
            line-height: 1.6;
        }
        
        .read-more {
            display: inline-block;
            padding: 10px 20px;
            background: linear-gradient(to right, #2575fc, #1a68e8);
            color: white;
            text-decoration: none;
            border-radius: 6px;
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(37, 117, 252, 0.3);
        }
        
        .read-more:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(37, 117, 252, 0.4);
        }
        
        .loading {
            text-align: center;
            padding: 50px;
            font-size: 1.3rem;
            color: #2575fc;
        }
        
        .loading-spinner {
            border: 5px solid #f3f3f3;
            border-top: 5px solid #2575fc;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .error {
            text-align: center;
            padding: 30px;
            background: linear-gradient(to right, #ffecec, #ffd6d6);
            color: #e74c3c;
            border-radius: 12px;
            margin: 20px 0;
            box-shadow: 0 4px 15px rgba(231, 76, 60, 0.15);
        }
        
        .news-category {
            display: inline-block;
            padding: 6px 14px;
            background: linear-gradient(to right, #e6f2ff, #d4e6ff);
            color: #2575fc;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
            margin-bottom: 15px;
        }
        
        @media (max-width: 768px) {
            .news-container {
                grid-template-columns: 1fr;
            }
            
            h1 {
                font-size: 2.2rem;
            }
            
            .controls {
                gap: 10px;
            }
            
            .category-btn {
                padding: 10px 18px;
                font-size: 0.9rem;
            }
        }
        
        .footer {
            text-align: center;
            margin-top: 50px;
            padding: 25px;
            color: #6c757d;
            font-size: 0.95rem;
            border-top: 1px solid #eaeff5;
        }
        
        .solution-info {
            background: #e6f7ff;
            border-radius: 12px;
            padding: 20px;
            margin: 30px 0;
            border-left: 4px solid #1890ff;
        }
        
        .solution-title {
            color: #1890ff;
            margin-bottom: 15px;
            font-size: 1.3rem;
        }
        
        .retry-btn {
            display: inline-block;
            margin-top: 15px;
            padding: 8px 20px;
            background: linear-gradient(to right, #1890ff, #096dd9);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        .retry-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(24, 144, 255, 0.3);
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>四川化工职业技术学院</h1>
            <p class="description">实时获取最新新闻资讯，涵盖多个新闻类别</p>
        </header>
        
        <div class="solution-info">
            <h3 class="solution-title">大数据呈现技术</h3>
            <!-- <p>此应用使用两种方法解决跨域问题：</p> -->
            <!-- <ol>
                <li>首选JSONP技术（API支持时）</li>
                <li>备选CORS代理服务器</li>
            </ol>
            <p>当前使用：<strong id="method-used">JSONP方法</strong></p> -->
        </div>
        
        <div class="controls">
            <button class="category-btn active" data-category="top">头条</button>
            <button class="category-btn" data-category="shehui">社会</button>
            <button class="category-btn" data-category="guonei">国内</button>
            <button class="category-btn" data-category="guoji">国际</button>
            <button class="category-btn" data-category="yule">娱乐</button>
            <button class="category-btn" data-category="tiyu">体育</button>
            <button class="category-btn" data-category="junshi">军事</button>
            <button class="category-btn" data-category="keji">科技</button>
        </div>
        
        <div id="loading" class="loading">
            <div class="loading-spinner"></div>
            正在加载新闻数据...
        </div>
        
        <div id="error" class="error" style="display: none;">
            加载新闻数据时出错，请稍后再试。
        </div>
        
        <div id="news-container" class="news-container">
            <!-- 新闻卡片将通过JS动态生成 -->
        </div>
        
        <div class="footer">
            <p>数据来源：聚合数据 </p>
            <p>当前时间：<span id="current-time"></span></p>
        </div>
    </div>

    <script>
    $(document).ready(function() {
        // 显示当前时间
        function updateTime() {
            // ecmascrpit=====协议
            const now = new Date();
            const timeString = now.toLocaleString('zh-CN', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit',
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit',
                hour12: false
            });
            // 改变页面结构====》dom
            $('#current-time').text(timeString);
        }
        updateTime();
        setInterval(updateTime, 1000);
        
        // 默认加载头条新闻
        loadNews('top');
        
        // 分类按钮点击事件
        $('.category-btn').click(function() {
            $('.category-btn').removeClass('active');
            $(this).addClass('active');
            var category = $(this).data('category');
            loadNews(category);
        });
        
        // 加载新闻函数
        function loadNews(category) {
            // 显示加载状态
            // BOM 浏览器对象模型
            alert("正在加载中，请等候")
            $('#loading').show();
            $('#error').hide();
            $('#news-container').empty();
            
            // 使用JSONP解决跨域问题（首选方法）
            tryJsonp(category);
        }
        
        // 尝试使用JSONP方法
        function tryJsonp(category) {
            console.log("尝试使用JSONP方法...");
            $('#method-used').text('JSONP方法');
            
            $.ajax({
                url: 'https://v.juhe.cn/toutiao/index',
                type: 'GET',
                dataType: 'jsonp',
                jsonp: 'callback',
                data: {
                    key: '248c75f53239f1a4faffeb126c031cf7',
                    type: category
                },
                success: function(response) {
                    $('#loading').hide();
                    
                    if (response.error_code === 0) {
                        var newsList = response.result.data;
                        renderNews(newsList);
                    } else {
                        showError('API错误: ' + response.reason);
                    }
                },
                error: function(xhr, status, error) {
                    console.log("JSONP失败，尝试CORS代理方法...");
                    // JSONP失败时尝试使用CORS代理
                    tryCorsProxy(category);
                },
                timeout: 5000 // 5秒超时
            });
        }
        
        // 尝试使用CORS代理方法
        function tryCorsProxy(category) {
            console.log("尝试使用CORS代理方法...");
            $('#method-used').text('CORS代理方法');
            
            // 使用可靠的CORS代理绕过跨域限制
            const proxyUrl = 'https://api.allorigins.win/raw?url=';
            const targetUrl = encodeURIComponent(`http://v.juhe.cn/toutiao/index?key=248c75f53239f1a4faffeb126c031cf7&type=${category}`);
            
            $.ajax({
                url: proxyUrl + targetUrl,
                type: 'GET',
                dataType: 'json',
                success: function(response) {
                    $('#loading').hide();
                    
                    if (response.error_code === 0) {
                        var newsList = response.result.data;
                        renderNews(newsList);
                    } else {
                        showError('API错误: ' + (response.reason || '未知错误'));
                    }
                },
                error: function(xhr, status, error) {
                    $('#loading').hide();
                    showError('请求失败: ' + error + '。请尝试刷新页面或稍后再试。');
                }
            });
        }
        
        // 渲染新闻列表
        function renderNews(newsList) {
            var container = $('#news-container');
            container.empty();
            
            if (!newsList || newsList.length === 0) {
                container.html('<div class="error">未找到相关新闻</div>');
                return;
            }
            
            // 使用模拟图片URL（因为API返回的图片URL可能无法访问）
            const imagePlaceholders = [
                'https://via.placeholder.com/600x400/2575fc/ffffff?text=新闻图片',
                'https://via.placeholder.com/600x400/6a11cb/ffffff?text=新闻图片',
                'https://via.placeholder.com/600x400/1a68e8/ffffff?text=新闻图片'
            ];
            
            newsList.forEach(function(news, index) {
                // 使用模拟图片URL
                const imageUrl = imagePlaceholders[index % imagePlaceholders.length];
                
                var newsCard = `
                    <div class="news-card">
                        <div class="news-image">
                            <img src="${imageUrl}" alt="${news.title}" style="width:100%;height:100%;">
                        </div>
                        <div class="news-content">
                            <h3 class="news-title">${news.title}</h3>
                            <div class="news-meta">
                                <span>${news.author_name || '未知来源'}</span>
                                <span>${news.date || '未知日期'}</span>
                            </div>
                            <div class="news-category">${news.category || '新闻'}</div>
                            <p class="news-description">${news.description || '暂无描述，请点击阅读全文查看详情。'}</p>
                            <a href="${news.url}" class="read-more" target="_blank">阅读全文</a>
                        </div>
                    </div>
                `;
                container.append(newsCard);
            });
        }
        
        // 显示错误信息
        function showError(message) {
            alert("请求失败")
            $('#error').html(`
                <h3>请求失败</h3>
                <p>${message}</p>
                <p>请尝试以下解决方案：</p>
                <ul>
                    <li>刷新页面重试</li>
                    <li>检查网络连接</li>
                    <li>稍后再试</li>
                </ul>
                <button class="retry-btn" onclick="location.reload()">刷新页面</button>
            `).show();
        }
    });
    </script>
</body>
</html>
